/*
  学习目标：使用subscribe监听仓库数据的变化
  语法: const 解除监听的函数 = store.subscribe(() => {数据变化时, 自动执行})
  注意:
    1. 先监听, 后dispatch
    2. 解除监听后, 不再触发
*/

import { legacy_createStore as createStore } from 'redux';

const store = createStore(function (state = { count: 0 }, action) {
  console.warn('action  ----->  ', state.count, action);
  if (action.type === 'add') {
    return { count: state.count + 1 };
  }

  if (action.type === 'des') {
    return { count: state.count - 1 };
  }

  return state;
});

const unFn = store.subscribe(() => {
  console.log('仓库数据变化会自动执行  ----->  ', store.getState());
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });

unFn();

store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
